package mysql

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"order_srv/appconfig"
	"sync"
)

var (
	Db   *gorm.DB
	err  error
	once sync.Once
)

func InitMySQL() {
	app := appconfig.AppConf.MySQLConfig
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", app.User, app.Password, app.Host, app.Port, app.Database)
	once.Do(func() {
		Db, err = gorm.Open(mysql.Open(dsn))
		if err != nil {
			fmt.Println("数据库连接失败")
			return
		}
	})
	Db.AutoMigrate(new(Order), new(OrderGoods), new(OrderAddress))
}
